<?php
/************************************************************************/
/* FrameWork  test preview                                              */
/* ==================================================================== */
/*                                                                      */
/* This program is free software. You can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 2 of the License.       */
/************************************************************************/

$ta=$_POST['ta'];
$author=$_POST['author'];

    if ($page==""){
	$page="FrontPage";
    }
    else{
	$page=str_replace('..',"",$page);
	$page=str_replace('/',"",$page);
    }

    //the first start
    if (file_exists("datas/applications/".apptitle($appname)."/pages/FrontPage")==0)
	copy("applications/$appname/default/FrontPage.$lang" ,"datas/applications/".apptitle($appname)."/pages/FrontPage");
    //if page not exist create default
    if (file_exists("datas/applications/".apptitle($appname)."/pages/$page")==0){
	$fp=fopen("datas/applications/".apptitle($appname)."/pages/$page","wb");
	fwrite($fp,"clean page\n");
	fwrite($fp,"please edit this page\n");
	fclose($fp);
    }


    if ($datepage=="")$datepage=filemtime("datas/applications/".apptitle($appname)."/pages/$page");

    if ($ta!='' && $datepage==filemtime("datas/applications/".apptitle($appname)."/pages/$page")){
	//trasform " in '  char for working reason: have you a better idea?
	$ta=str_replace('\"',"'",$ta);
	$ta=str_replace("\\","",$ta);
	//$ta=rawurlencode($ta); if you want encrypt old page

	//backup currentpage
	$i="0";
	while(file_exists("datas/applications/".apptitle($appname)."/history/$page.".$i)!=0)$i++;
	if (file_exists("datas/applications/".apptitle($appname)."/pages/$page"))
	    copy("datas/applications/".apptitle($appname)."/pages/$page","datas/applications/".apptitle($appname)."/history/$page.$i");

	//author of the page
	if ($author=="")$author="anonymous";
	$author="$author ip:".getenv('REMOTE_ADDR')."\n";

	//save new page updated
	$fp=fopen("datas/applications/".apptitle($appname)."/pages/$page",'wb');
	fwrite ($fp,$author);
	fwrite($fp,$ta);
	fclose($fp);

	//reset the $datepage with a new param
	$datepage=filemtime("datas/applications/".apptitle($appname)."/pages/$page");

	//to do trasform backuppage in diff file

    }

    //load the requested page
    if (file_exists("datas/applications/".apptitle($appname)."/pages/$page")){
        $ta=file("datas/applications/".apptitle($appname)."/pages/$page");
	}
    else
    // the page is new page please edit it
	$ta[0]="please edit this page";

    print("<table width='90%'><tr>\n");
    print("<td><a href=\"javascript:history.go(-1);\">indietro</a></td>");
    print("<td><a href='index.php?appname=$appname&page=FrontPage'>FrontPage</a></td>");
    print("<td>search</td>");
    print("<td><form enctype='multipart/form-data' action='index.php?appname=$appname&action=search' method='post'>
	    <input type='text' name='find' size=40 />
	    <input type='submit' value='Search' /></form></td>");

    printf("</tr></table>\n");
    printf("<a href='index.php?appname=$appname&page=$page&action=link'><h1>$page</h1></a>");
    ?>
    <hr noshade>
    <?
	//verify if the last page was updated
	if($datepage!=filemtime("datas/applications/".apptitle($appname)."/pages/$page")){
	    echo"<font color=\"ff0000\"><h1>An error occured in update the page</h1>\n";
	    echo"probably another user had update this page when you edit<br />\n";
	    echo"sorry for inconvenience: i suggest to repeat the operation<br /></font>\n";
	    echo"yourdatepage $datepage : the datepage of page ".filemtime("datas/applications/".apptitle($appname)."/pages/$page");
	    echo" <hr />\n";
	}
	echo"<span>\n";
	for($i=1;$i<count($ta);$i++)
	    echo(rawurldecode($ta[$i]));
	echo"</span>";
    ?>
    <hr noshade>
    <table width= "90%" >
    <tr>
    <?
	//if page not exist create default
	if (file_exists("datas/applications/".apptitle($appname)."/pages/$page")==0){
	    $fp=fopen("datas/applications/".apptitle($appname)."/pages/$page");
	    fwrite($fp,"clean page\n");
	    fwrite($fp,$ta[0]);
	    fclose($fp);
	}
	//edit button
	if (user_getuser()){
	    printf("<td>[<a href='index.php?appname=$appname&action=list'>rec changes</a>]</td>\n");

	    if ((defined('LIBGROUP') && group_getusergroup("vwiki",user_getuser())) || admin_isadmin()){
		if (is_writable("datas/applications/".apptitle($appname)."/pages/$page")){
		    printf("<td >[<a href='index.php?appname=$appname&action=edit&page=$page'>edit</a>]</td>");
		}
	    }
	    //admin button
	    if (!file_exists("datas/applications/".apptitle($appname)."/admin.flag"))
		touch("datas/applications/".apptitle($appname)."/admin.flag");
	    if(admin_isadmin()){
		if (is_writable("datas/applications/".apptitle($appname)."/admin.flag"))
		    printf("<td >[<a href='index.php?appname=$appname&page=$page&action=admin'>admin</a>]</td>\n");
	    }
	    if (is_writable("datas/applications/".apptitle($appname)."/pages/$page")){
		//diff button
		printf("<td >[<a href='index.php?appname=$appname&action=diff&page=$page'>diff</a>]</td>");
	    }
	}
	//last update button
	printf("<td ><font size=2><b>last change:</b> ".date("j F Y g:i a",filemtime("datas/applications/".apptitle($appname)."/pages/$page"))."</font></td>");
	printf("<td></td>");
	//powered by CFKeditor
	printf("<td ><font size=2><b>vwiki-FW</b><br /> powered by <br/><b>CFKeditor</b></font></td>\n");
	printf("</tr></div>");
	//button of recent changes

?>
    </table>
